<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/ssm/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/ssm/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">课程名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="courseName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--行工具栏模板-->
        <script type="text/html" id="lineBar">
            <div class="layui-clear-space">
                <a class="layui-btn layui-btn-xs layui-bg-green" lay-event="edit">
                    <i class="layui-icon layui-icon-top"></i>编辑
                </a>
                <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">
                    <i class="layui-icon layui-icon-delete"></i>删除</a>
            </div>
        </script>
        <!--头部工具栏模板-->
        <script type="text/html" id="headerBar">
            <div class="layui-clear-space">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>


    </div>
</div>
<script src="/ssm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        let $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        // 已知数据渲染
        let host = "http://localhost:8080/ssm/course/getCoursePicture?img="
        table.render({
            elem: '#currentTableId',
            url: '/ssm/course/page',
            toolbar: '#headerBar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'courseName', title: '课程名称'},
                {
                    field: 'img', title: '图片', minWidth: 160, event:'editImg', templet: function (row) {
                        return '<div>' +
                            '<img src="'+host+row.img+'"  style="width: 80px;height: 80px;">'+
                            '</div>'
                    }
                },
                {field: 'type', title: '课程类型'},
                {field: 'price', title: '价格'},
                {field: 'coachName', title: '教练'},
                {field: 'comment', title: '课程简介'},
                {field: 'createTime', title: '创建时间'},
                {title: '操作', minWidth: 200, toolbar: '#lineBar', align: "center"}
            ]],
            limits: [5, 10, 15],
            limit: 5,
            page: true,
            skin: 'line'
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let searData =  data.field
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    courseName : searData.courseName
                }
            }, 'data');

            return false;
        });




        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let index = layer.open({
                    title: '新增',
                    type: 2,
                    shade: 0.6,
                    shadeClose: true,
                    area: ['30%', '70%'],
                    content: 'add.html',
                    end: function () {
                        table.reload("currentTableId")  // 重载表格数据
                    }
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') {
                sessionStorage.setItem("course",JSON.stringify(data))
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'edit.html',
                    end:function (){
                        table.reload("currentTableId")
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    let img= data.img
                    $.post('/ssm/course/delete',{img},function (res){
                        if (res.code==0){
                            layer.msg(res.msg,{icon:1},function (){
                                table.reload("currentTableId")
                            })
                        }else {
                            layer.msg(res.msg,{icon:2})
                        }
                    })
                    layer.close(index);
                });
            }else  if (obj.event === 'editImg'){
                sessionStorage.setItem("courseId",data.id);
                sessionStorage.setItem("img",data.img);
                let index = layer.open({
                    title: '图片上传',
                    type: 2,
                    shade: 0.6,
                    shadeClose: true,
                    area: ['30%', '70%'],
                    content: 'upload.html',
                    end: function () {
                        table.reload("currentTableId")  // 重载表格数据
                    }
                });
            }
        });


    });
</script>

</body>
</html>